﻿

var IntialactivityItems = function ($item, title, cls) {
    var str = "<ul class='sortable toolbox_ul li_style'>";
    var child
    child = "<li class='ui-state-default milestone_designtoolbox start' milestonetype=><img src='images/流程开始.png'class='absmiddle' />&nbsp;&nbsp;流程开始</li>";
    str = str + child;
    child = "<li class='ui-state-default milestone_designtoolbox end'><img src='images/流程结束.png'class='absmiddle' />&nbsp;&nbsp;流程结束</li>";
    str = str + child;
    child = "<li class='ui-state-default activity_designtoolbox'><img src='images/审核环节.png'class='absmiddle' />&nbsp;&nbsp;审核环节</li>";
    str = str + child;
    child = "<li class='ui-state-default activity_designtoolbox'><img src='images/审批环节.png'class='absmiddle' />&nbsp;&nbsp;审批环节</li>";
    str = str + child;
    child = "<li class='ui-state-default activity_designtoolbox'><img src='images/知会环节.png'class='absmiddle' />&nbsp;&nbsp;知会环节</li>";
    str = str + child;
    child = "<li class='ui-state-default activity_designtoolbox'><img src='images/执行环节.png'class='absmiddle' />&nbsp;&nbsp;执行环节</li>";
    str = str + child;
    child = "<li class='ui-state-default activity_designtoolbox'><img src='images/ewewe_095.png'class='absmiddle' />&nbsp;&nbsp;董事会黑盒</li>";
    str = str + "</ul>";
    var $str = $(str).appendTo($item);
    $str.find('img').css('background', 'url("images/bg.editor.li.1.jpg") no-repeat scroll left top transparent')
    .css('border-bottom', ' 1px solid #CBCBCB').css('padding','10px');
    //     background: url("../images/bg.editor.li.1.jpg") no-repeat scroll left top transparent;
    //    border-bottom: 1px solid #CBCBCB;
    //    padding: 10px;
    IntialItemHoverStyle($item);
};


var IntialItemHoverStyle = function ($item) {
    $item.find('.ui-state-default').each(function () {
        var $li = $(this);
        $li.mouseover(function () {
            $(this).hover(function () {
                $li.addClass('ui-state-highlight');
                $li.removeClass('ui-state-default');
            },
            function () {
                $li.addClass('ui-state-default');
                $li.removeClass('ui-state-highlight');
            });
        });

    });
};

var InitialDesignToolbox = function ($designToolbox) {
    $designToolbox
    //    .append('<p class="conditionDesignTitle"><a href="#">待选条件</a></p><div id="divConditionItems"></div>')
    .append('<div id="divActivityItems" class="pageEditorLeftPanelBoxList"></div>')
    //.append('<div id="divOthersItems"></div>')

    //var $conditionItems = $("#divConditionItems");
    var $activityItems = $("#divActivityItems");
    //var $othersItems = $("#divOthersItems");
    //IntialconditionItems($conditionItems, "待选条件", "conditionItem");
    IntialactivityItems($activityItems, "待选环节", "activityItem");
    //IntialothersItems($othersItems, "环节容器", "othersItems");



    $(".sortable").sortable({ helper: "clone",
        appendTo: "body", connectWith: ".sortableOtherItem"
        , over: function (event, ui) {

        },
        receive: function (event, ui) {
            $("#container").droppable('enable');
        }
    })
    $(".sortableOtherItem").sortable({ helper: function (event) {
        return $("<img src='/images/okay.png' style='width:60px'/>");
    },
        appendTo: "body"
    });
    $(".sortable").disableSelection();

    $(".sortable").sortable({ helper: "clone", appendTo: "body", connectWith: ".container_othersItems .sortable" });
    $(".sortable").disableSelection();


    //画布响应drop 事件 
    $("#container").droppable({
        accept: 'li',
        drop: function (event, ui) {
            var $container = $("#container");
            var position = $container.offset();
            x = event.pageX - position.left;
            y = event.pageY - position.top;
            var $draggable = $(ui.draggable);
            $draggable.unbind('hover');
            var c;
            var $addedDiv
            if ($draggable.hasClass('activity_designtoolbox')) {
                c = { wrapper: 'pageEditorElementItem', container: 'pageEditorElementItemBox' }
                $addedDiv = $('<div class="' + c.wrapper + '"><div class="' + c.container + '"></div></div>').appendTo($container);
            }
            else if ($draggable.hasClass('milestone_designtoolbox start')) {
                c = { img: 'pageEditorElementMilestonestartimg', wrapper: 'pageEditorElementMilestone', container: 'pageEditorElementMilestoneBox' }
                $addedDiv = $('<div class="' + c.wrapper + '"><div class="' + c.container + '"><img class="pageEditorElementMilestoneimg" src="images/流程开始.png"/></div></div>').appendTo($container);
            }
            else if ($draggable.hasClass('milestone_designtoolbox end')) {
                c = { img: 'pageEditorElementMilestoneendimg', wrapper: 'pageEditorElementMilestone', container: 'pageEditorElementMilestoneBox' }
                $addedDiv = $('<div class="' + c.wrapper + '"><div class="' + c.container + '"><img class="pageEditorElementMilestoneimg" src="images/流程结束.png"/></div></div>').appendTo($container);
            }

            $addedDiv.data('containerclass', c);
            $addedDiv.css('position', 'absolute').css('left', x).css('top', y);
            $addedDiv.addInterfaces();

            $addedDiv.draggable({ drag: function () {
                var $dragtarget = $(ui.helper);
                var line
                
                var alllines = $addedDiv.attrFunctions('l', "all");

               
                alllines = $addedDiv.attrFunctions('l', "all");
                for (var i = 0; i < alllines.length; i++) {
                    line = alllines[i];



                    var start = line.getStart();

                    var end = line.getEnd();
                    
                    var indexstart
                    var indexend
                    if (start) {
                        if (start[0] == $addedDiv[0]) {
                            indexstart = start.index(start.parent().children('.container_othersItems'));
                        }
                    }
                    if (end) {
                        if (end[0] == $addedDiv[0]) {
                            indexend = end.index(end.parent().children('.container_othersItems'));
                        }
                    }


                    var inedxdrag = $addedDiv.index($dragtarget.parent().children('.pageEditorElementItem'));
                    if (inedxdrag == indexstart) {
                        line.start(start);
                    }
                    if (inedxdrag == indexend) {
                        line.end(end);
                    }
                }



            }
            });


        }
    });
};


//点击环节编辑按钮
var raiseActivityEditor = function ($target) {
    var $dialog = $("#dialog_activity");
    var $actname = $dialog.find("#txtActName");
    var $actDuration = $dialog.find("#txtActDuration");
    var $actApprover = $dialog.find("#txtActApprover");
    var $approveType = $dialog.find("#ddlApproveType");
    var $rdSerialTrue = $dialog.find("#rdSerialTrue");
    var $rdSerialFalse = $dialog.find("#rdSerialFalse");

    var mode = $target.attr('activityUpdateMode');
    if (mode) {
        $target.attr("activityUpdateMode", "editing");

        $actname.val($target.attr("actname"));
        $actDuration.val($target.attr("actDuration"));
        $actApprover.val($target.attr("actApprover"));
        $approveType.val($target.attr("approveType"));
        if ($target.attr("actSerial")) {
            $rdSerialTrue.attr("checked", "checked");
        }
        else {
            $rdSerialFalse.attr("checked", "checked");
        }

    }
    else {
        $target.attr("activityUpdateMode", "create");
        $actname.val($target.attr(""));
        $actDuration.val($target.attr(""));
        $actApprover.val($target.attr(""));
        $approveType.val($target.attr(""));
    }
    $("#dialog_activity").dialog("open");
}

//点击环节删除按钮
var raiseActivityDelete = function ($target, $childDiv) {
    if (confirm('确定要删除次子项？')) {
        $target.remove();
        updateInterfacePosition($childDiv);
    }
}
//closeline
//function closeline() {
//    isline = 0;
//    isendline = 0;
//    isstartline = 0;
//    $(".line" + currentline + "").remove();

//}
//
//function rightcilckline() {
//    if (isline == 1) {
//        window.document.oncontextmenu = closeline;
//    }
//    else { window.document.oncontextmenu = ""; }

//}
function deleteitems($addedDiv) {
    if (confirm('确定要删除此项？')) {
        $addedDiv.remove();
        for (i = 0; i < linenum; i++) {

            $("#container").find(".line" + i + "").each(function () {
                var $this = $(this);
                px = $this.attr("startx");
                py = $this.attr("endx");

            });
            if (px == $addedDiv.get(0).id || py == $addedDiv.get(0).id) {
                $(".line" + i + "").remove();
            }

        }
    }

}